import config
from api_providers.provider_factory import ProviderFactory
from core.conversation_manager import ConversationManager


class LanguageChangeService:
    def __init__(self):
        self.config = config.Config
        self.provider = ProviderFactory.get_provider()
        self.conversation_manager = ConversationManager()

    def translate(self, text, target_language):
        return self.provider.translate(text, target_language)

    def converse(self, conversation_id, message):
        # 获取当前会话的轮次数
        current_turns = self.conversation_manager.get_turns(conversation_id)
        max_turns = self.config.max_turns

        # 如果当前轮次已经达到最大轮次，返回提示信息
        if current_turns >= int(max_turns):
            return "The conversation has reached the maximum number of turns."

        # 否则继续对话
        self.conversation_manager.add_message(conversation_id, message)
        history = self.conversation_manager.get_history(conversation_id)
        response = self.provider.converse(conversation_id, history)
        self.conversation_manager.add_message(conversation_id, response)
        return response
